<?php

    /**
    * This is the model class for table "item_price".
    *
    * The followings are the available columns in table 'item_price':
    * @property string $id
    * @property string $name
    * @property string $name_en
    * @property integer $laundry
    * @property integer $dry
    * @property integer $iron
    * @property string $group
    * @property integer $index
    */
    class Item extends Model
    {
        public $prices = array(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,90,100,110,120,130,140,150);

        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return ItemPrice the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'item';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('name, name_en, iron, laundry_dry, group, unit', 'required'),
                array('iron, laundry_dry', 'numerical', 'integerOnly'=>true),
                array('name, name_en', 'length', 'max'=>255),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, name, name_en, iron, laundry_dry', 'safe', 'on'=>'search'),
            );
        }

        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
            );
        }

        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'name' => Yii::t('app', 'Tên đồ'),
                'name_en' => Yii::t('app', 'Item\'s name'),
                'iron' => Yii::t('app', 'Là hơi'),
                'laundry_dry' => Yii::t('app', 'Giặt khô & là hơi'),
                'group' => Yii::t('app', 'Nhóm'),
                'unit' => Yii::t('app', 'Đơn vị'),
            );
        }
        
        public function getAdminUrl($action = 'update', $id = NULL){
            return Yii::app()->createAbsoluteUrl('/admin/item/'.$action, array('id' => $id ? $id : $this->id));
        }

        public function getItemData(){
            $items = $this->findAll(new CDbCriteria(array(
                'order' => '`group` ASC, `unit` ASC, `index` ASC'
            )));
            $item_prices = array();

            foreach($items as $item){
                $item_prices[$item->group][$item->id] = $item;
            }
            return $item_prices;
        }
        public function getItemDataArray(){
            $items = $this->findAll(new CDbCriteria(array(
                'order' => '`group` ASC, `unit` ASC, `index` ASC'
            )));
            $item_prices = array();

            foreach($items as $item){
                $item_prices[$item->group][$item->id] = $item->attributes;
                $item_prices[$item->group][$item->id]['laundry'] = $item->group == 'TIME' ? $this->price_package_laundry : 0;        
                $item_prices[$item->group][$item->id]['dry'] = $item->group == 'TIME' ? $this->price_package_dry : 0;        
            }
            
//            $blanketKgPrice = $item_prices['BLANKET'][49]['laundry_dry'];
//            unset($item_prices['BLANKET'][49]);
//            
//            $lastItemInBlanketGroup = 10000;//end($item_prices['BLANKET']);

//            for($i = 1; $i <= 20; $i++){
//                $item_prices['BLANKET'][$lastItemInBlanketGroup + $i] = array(
//                    'id' => $lastItemInBlanketGroup + $i,
//                    'name' => "{$i} Kg - Chăn Lông/Len Trung Quốc",
//                    'iron' => null,
//                    'laundry_dry' => $blanketKgPrice * $i,
//                    'group' => 'BLANKET',
//                ); 
//            }

            
            return $item_prices;
        }


        public function getUnitData(){
            return array(
                'UNIT'  => Yii::t('app', 'Cái'),
                'KG'  => Yii::t('app', 'Kg'),
                'ITEM'  => Yii::t('app', 'Con'),
            );
        }
        public function getUnitLabel($key = ''){
            $key = $key ? $key : $this->unit; 
            $data = $this->unitData;
            return isset($data[$key]) ? $data[$key] : null;
        }

        public function getGroupData(){
            return array(
                'TIME'  => Yii::t('app', 'Lượt'),
                'SHIRT'  => Yii::t('app', 'Áo'),
                'PANTS'  => Yii::t('app', 'Quần'),
                'DRESS'  => Yii::t('app', 'Váy'),
                'BLANKET'  => Yii::t('app', 'Chăn Ga Gối'),
                'BLIND'  => Yii::t('app', 'Rèm'),
                'OTHER'  => Yii::t('app', 'Khác'),
            );
        }
        public function getGroupLabel($key = ''){ 
            $key = $key ? $key : $this->group;
            $groupData = $this->groupData;
            return isset($groupData[$key]) ? $groupData[$key] : null;
        }


        public function getColorData(){
            return array(
                'black' => 'đen',
                'white' => 'trắng',
                'gray' => 'ghi',
                'red' => 'đỏ',
                'pink' => 'hồng',
                'blue' => 'xanh da trời',
                'green' => 'xanh lá cây',
                'yellow' => 'vàng',
                'violet' => 'tím',
                'brown' => 'nâu',
            );
        }
        public function getColorLabel($key){ 
            $colorData = $this->colorData;
            return isset($colorData[$key]) ? $colorData[$key] : null;
        }


        public function getGenderData(){
            return array(
                'male' => 'nam',
                'female' => 'nữ',
                'child' => 'trẻ em',
            );
        }
        public function getGenderLabel($key){ 
            $genderData = $this->genderData;
            return isset($genderData[$key]) ? $genderData[$key] : null; 
        }

        public function getPriceData(){
            $price_data = array(0   => '---');
            foreach($this->prices as $p){
                $price_data[$p] = $p.' k';
            }
            return $price_data;
        }

        /**
        * Tra ve ten day du cua do thong qua info cua bill
        * 
        * @param mixed $info
        */
        public function formatInfo(& $info){
            foreach($info['items'] as & $item){
                $itemPrice = $this->findByPk($item['id']);

                $array = array($itemPrice->name);
                if($item['gender']) $array[] = $this->getGenderLabel($item['gender']);
                if($item['color']) $array[] = $this->getColorLabel($item['color']);
                if($item['more_info']) $array[] = $item['more_info'];

                $item['name'] = implode(', ', $array);
            }
        }

        public function getPrice_package_laundry(){
            $price_package_laundry = Yii::app()->params->price_package_laundry;
            if(Yii::app()->params->promo['active']){
                $price_package_laundry = Yii::app()->params->promo['price_package_laundry'];    
            }
            return $price_package_laundry;
        }

        public function getPrice_package_dry(){
            $price_package_dry = Yii::app()->params->price_package_dry;
            if(Yii::app()->params->promo['active']){
                $price_package_dry = Yii::app()->params->promo['price_package_dry'];    
            }
            return $price_package_dry;
        }

}